package com.ygqh.baby.controller.admin;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.po.AdminSupplier;
import com.ygqh.baby.service.AdminSupplierService;
import com.ygqh.baby.utils.JsonUtils;

@Controller
@RequestMapping("/admin/supplier")
public class SupplierController  extends BaseController<Object, java.lang.Long>{
	
    @Autowired
	private AdminSupplierService adminSupplierService;	
	
	@RequestMapping("searchSupplier")
	@ResponseBody
	public Map<String,Object> searchSupplier(String q,QueryInfo queryInfo) {	
		
		ResultSet<AdminSupplier> resultSet=adminSupplierService.search(queryInfo,q);
		
		return resultSet.toDatatables(null);
	}
	@RequestMapping("searchSupplierList")
	@ResponseBody
	public List<AdminSupplier> searchSupplierList(String q) {	
		
		return adminSupplierService.find(q);
	}
	
	@RequestMapping("saveSupplier")
	@ResponseBody
	public Message saveSupplier(String supplier) {
		AdminSupplier adSupplier=JsonUtils.toObject(supplier, AdminSupplier.class);	
		if(adSupplier.getId()!=null){
			adSupplier.setUpdateBy(getSession().getTrueName());
			adSupplier.setUpdateTime(new Date());
		}else{
			adSupplier.setCreateBy(getSession().getTrueName());
			adSupplier.setCreateTime(new Date());
			
		}
		//判断供应商名称是否存在
		if(checkSupplierName(adSupplier.getSupplierName(),adSupplier.getId())>0)
			return Message.error("供应商名称不能重复");
	
	   return adminSupplierService.saveOrUpdate(adSupplier)==1?SUCCESS_MESSAGE:ERROR_MESSAGE;
	
	}
	private int checkSupplierName(String supplierName,Long id){
		return adminSupplierService.checkSupplierName(supplierName,id);
	}
	@RequestMapping("getSupplier")
	@ResponseBody
	public AdminSupplier getSupplier(Long id) {
		
	   return adminSupplierService.findById(id);
	
	}
	
}
